﻿using System;
using System.Collections;
using System.Web;
//**************************************************************************************************
// 系统名称	：沿海集团任务管理系统
// 文件名称	： UserHandle.cs
// 做成履历 ：
//      日期                    姓名            内容
//      2015-4-25       XXXXXXXXX           创建用户权限，功能处理类
//**************************************************************************************************
using System.Xml;
using System.Data;

namespace CRM.UTIL
{
    /// <summary>
    /// 用户权限，功能处理类
    /// </summary>
    [Serializable]
    public class UserHandle
    {
        public UserHandle() { }

        /// <summary>
        /// 初始化当前登陆角色权限列表
        /// </summary>
        /// <returns></returns>
        public static void InitPage()
        {
            //ArrayList Ulists = new ArrayList();//用户的页面权限

            //BLL.FacadeSystem.role_privilegeClass obj = new BLL.FacadeSystem.role_privilegeClass();
            //MODEL.DataStruct.role_privilege.Getrole_privilege_ stu = new MODEL.DataStruct.role_privilege.Getrole_privilege_();

            //string role_id_ = Convert.ToString(SessionBox.GetUserSession().RoleId);
            //stu.ROLE_ID_VALUE = role_id_;
            
            //DataTable dt = obj.Getrole_privilege_(ref stu); //角色权限
            //foreach (DataRow dr in dt.Rows)
            //{
            //    string priv_code = Convert.ToString(dr["priv_code_"]);
            //    Ulists.Add(priv_code);
            //}

            SessionBox.RemovePageAuthority(); //移除页面权限
         //   SessionBox.CreatePageAuthority(Ulists); //创建用户页面权限列表
        }

        /// <summary>
        /// 验证权限  :批准操作
        /// </summary>
        /// <param name="pa_tag">权限标识</param>
        /// <returns></returns>
        public static bool ValidationHandle(string pa_tag)
        {
            bool ret = false;
            if (SessionBox.CheckUserSession())
            {
                ArrayList Ulist = SessionBox.GetPageAuthority(); //用户页面权限
                if (Ulist == null || Ulist.Count == 0) { InitPage(); }

                for (int j = 0; j < Ulist.Count; j++)
                {
                    if (Ulist[j].ToString().ToLower() == pa_tag.ToLower())//是否在用户权限表中
                    {
                        ret = true;
                        break;
                    }
                }
            }
            return ret;
        }

        /// <summary>
        ///  检测是否是最高的权限，true：不受到权限限制，false：受到权限限制
        /// </summary>
        /// <returns></returns>
        public static bool CheckAdminUser()
        {
            if (SessionBox.GetUserSession().IsAdmin.ToString() == "admin")
                return true;
            else
                return false;
        }

        /// <summary>
        /// 返回对应的状态字符串
        /// </summary>
        /// <param name="n">状态标识</param>
        /// <returns></returns>
        public static string ReturnState(string isActive)
        {
            string ret = "";
            switch (isActive)
            {
                case "Y":
                    ret = "允许登录";
                    break;
                case "N":
                    ret = "帐号已经冻结，禁止登录";
                    break;
            }
            return ret;
        }

        public static string GetDefaultHomePage(object defaultHomePage)
        {
            const string homePage = "/Default.aspx";
            var xmlDoc = new XmlDocument();
            xmlDoc.Load((new System.Web.UI.Page()).Server.MapPath("/ConfigData/DefaultHomePage.xml"));
            var node = xmlDoc.SelectSingleNode("/Pages/Page[@id='" + defaultHomePage + "']");
            if (node != null && (node.Attributes != null && (node.Attributes["value"] != null)))
                return node.Attributes["value"].Value;
            return homePage;
        }
    }
}
